import { createRouter, createWebHashHistory } from 'vue-router'
import { defineAsyncComponent } from 'vue'
const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            component: defineAsyncComponent(() => import('../views/Home.vue') ),
            children: [
                {
                    path: '/helloWorld',
                    component: defineAsyncComponent(() => import('../views/HelloWorld.vue')),
                    meta: {
                        title: '聊一聊',
                    },
                },
                {
                    path: '/menuList',
                    component: defineAsyncComponent(() => import('../views/MenuList.vue')),
                    meta: {
                        title: '菜单',
                        notLogin: false
                    },
                },
                {
                    path: '/gen',
                    component: defineAsyncComponent(() => import('../views/gen/index.vue')),
                    meta: {
                        title: '在线开发',
                        notLogin: false
                    },
                }
            ]
        },
        {
            path: '/login',
            name: 'Login',
            component: defineAsyncComponent(() => import('../views/Login.vue')),
            meta: {
                title: '登录',
                notLogin: true
            },
        },
    ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const isAuthenticated = localStorage.getItem('satoken') // 替换为你的认证逻辑
    console.log("isAuthenticated", isAuthenticated, to);
    if (!to.meta.notLogin && !isAuthenticated) {
        next('/login')
    } else {
        next()
    }
})
export default router